import 'dart:async';
import 'dart:math' as math;

import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:flutter_fireworks/fire/firework.dart';


class FireworkEffect extends FlameGame {
  final math.Random random = math.Random();

  final int particleCount = 150;
  final double minParticleSize = 3;
  final double maxParticleSize = 9;

  final List<Firecracker> fireworks = [];
  final List<List<Particle>> explosions = [];
  int nextFirecrackerId = 0;

  late bool isRunning;


  FireworkEffect(): super(
    camera: CameraComponent.withFixedResolution(width: 1080, height: 1920)
  );

  void _generateFireworks() {
    Future.doWhile(() async {
      if (paused) {
        await Future.delayed(1.seconds);
        return isRunning;
      }
      int times = random.nextIntBetween(4, 10);
      for (int i = 0; i < times; i++) {
        final x = random.nextDouble() * 1080;
        final targetY = random.nextDouble() * 1920 / 3 + 1920 / 4;
        final firecracker = Firecracker(nextFirecrackerId++, x, 1720, targetY, false,
            false, random.nextColor(), []);
        world.add(Firework(firecracker));
        await Future.delayed(300.milliseconds);
      }
      await Future.delayed(2.seconds);
      return isRunning;
    });
  }

  List<Particle> createParticleList(
    double x,
    double y,
    Color baseColor,
  ) {
    List<Particle> particles = [];
    for (int i = 0; i < particleCount; i++) {
      // 随机角度和速度
      double angle = random.nextDouble() * 2 * math.pi;
      double speed = random.nextDouble() * 6 + 2;

      // 计算速度分量
      double velocityX = math.cos(angle) * speed;
      double velocityY = math.sin(angle) * speed;

      // 随机大小
      double radius = random.nextDouble() * (maxParticleSize - minParticleSize) + minParticleSize;

      Color color = baseColor;

      // 随机生命周期和旋转
      double maxLife = random.nextDouble() * 0.5 + 1.0;

      particles.add(Particle(x, y, velocityX, velocityY, radius, color,
          maxLife, maxLife, random.nextDouble() * 360, random.nextDouble() * 10 - 5));
    }
    return particles;
  }

  @override
  FutureOr<void> onLoad() {
    isRunning = true;
    super.onLoad();
    camera.viewfinder.anchor = Anchor.topLeft;
    _generateFireworks();
  }

  @override
  void onDispose() {
    isRunning = false;
    super.onDispose();
  }

  @override
  void lifecycleStateChange(AppLifecycleState state) {
    switch(state) {
      case AppLifecycleState.detached:
        break;
      case AppLifecycleState.resumed:
        resumeEngine();
        break;
      case AppLifecycleState.inactive:
        break;
      case AppLifecycleState.hidden:
        break;
      case AppLifecycleState.paused:
        pauseEngine();
        break;
    }
    super.lifecycleStateChange(state);
  }

}


// 粒子数据类，存储粒子的物理属性
class Particle {
  Particle(
    this.x,
    this.y,
    this.velocityX,
    this.velocityY,
    this.radius,
    this.color,
    this.life,
    this.maxLife,
    this.rotation,
    this.rotationSpeed,
  );

  double x;
  double y;
  double velocityX;
  double velocityY;
  double radius;
  Color color;
  double life;
  double maxLife;
  double rotation;
  double rotationSpeed;
}

// 花炮数据类
class Firecracker {
  Firecracker(
    this.id,
    this.x,
    this. y,
    this.targetY,
    this.isExploding,
    this.isFinished,
    this.color,
    this.trail,
  );

  int id;
  double x;
  double y;
  double targetY; // 目标高度，到达后自动爆炸
  bool isExploding;
  bool isFinished;
  Color color;
  List<Offset> trail; // 花炮尾迹
}